import java.util.Set;

import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.graph.UndirectedWeightedSubgraph;

/**
 * Can check if clusters are connected in a given graph.
 * @author odedmagg
 *
 */
public class ClusterConnectivityInspector {
	private SimpleWeightedGraph<String, DefaultWeightedEdge> _graph;
	
	public ClusterConnectivityInspector(SimpleWeightedGraph<String, DefaultWeightedEdge> g) {
		_graph =g; 
	}
	
	public boolean isClusterConnected(Cluster cluster) {
		if (_graph == null)
			throw new NullPointerException();
		
		Set<String> vertices = cluster.memberNames();
		UndirectedWeightedSubgraph<String, DefaultWeightedEdge> subgraph = 
			new UndirectedWeightedSubgraph<String, DefaultWeightedEdge>(_graph, vertices, null);
				
		ConnectivityInspector<String, DefaultWeightedEdge> connectivityInspector = 
			new ConnectivityInspector<String, DefaultWeightedEdge>(subgraph);
		
		return connectivityInspector.isGraphConnected();
	}
	

	
}
